package com.czxttkl.measure;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;

public class TCPService extends Service {
	static Bundle testInfo = new Bundle();
	static HashMap<String,String> testInfoMap = new HashMap<String,String>(30,(float)0.75);

	public static void establishTCP(){
		Log.d("TCP", "Client: Connecting...");
		Socket socket = new Socket();
        SocketAddress socketAddress = new InetSocketAddress("192.168.68.89",3333); 
         try {
            socket.connect(socketAddress,3000); 
			ObjectOutputStream out = new ObjectOutputStream(         
                    socket.getOutputStream());   
			bundleToHashMap();
			out.writeObject(testInfoMap);
			Log.d("TCP", "Client: Sending: '" + testInfoMap + "'");
			socket.close();
			new Thread(new MyThreadTcpSuccess()).start();
		} catch (SocketTimeoutException e) {
			Log.e("TCP", "S: Error", e);
			new Thread(new MyThreadTcpFailed()).start();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public void onCreate() {
		super.onCreate();
	}

	@SuppressLint({ "NewApi", "NewApi" })
	public void onStart(Intent intent, int startId) {
		testInfo = intent.getBundleExtra("testinfo");
		Log.d("TCPService", "has been started");
		// ���StrictMode�ĵ�
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());
		new Thread(new MyThreadTcp()).start();

	}

	public void onDestroy() {
		super.onDestroy();
	}
	public static HashMap<String,String> bundleToHashMap(){
		testInfoMap.put("ASU",testInfo.getString("ASU"));
		testInfoMap.put("BER",testInfo.getString("BER"));
		testInfoMap.put("BatteryLevel",testInfo.getString("BatteryLevel"));
		testInfoMap.put("BatteryScale",testInfo.getString("BatteryScale"));
		testInfoMap.put("BatteryHealth",testInfo.getString("BatteryHealth"));
		testInfoMap.put("BatteryTemperature",testInfo.getString("BatteryTemperature"));
		testInfoMap.put("BatteryTechnology",testInfo.getString("BatteryTechnology"));
		testInfoMap.put("BatteryVoltage",testInfo.getString("BatteryVoltage"));
		testInfoMap.put("SimState",testInfo.getString("SimState"));
		testInfoMap.put("ICCID",testInfo.getString("ICCID"));
		testInfoMap.put("SimOpeNum",testInfo.getString("SimOpeNum"));
		testInfoMap.put("SimOpeName",testInfo.getString("SimOpeName"));
		testInfoMap.put("SimISO",testInfo.getString("SimISO"));
		testInfoMap.put("IMEI",testInfo.getString("IMEI"));
		testInfoMap.put("IMSI",testInfo.getString("IMSI"));
		testInfoMap.put("PhoneType",testInfo.getString("PhoneType"));
		testInfoMap.put("MSISDN",testInfo.getString("MSISDN"));
		testInfoMap.put("NetworkOpeNum",testInfo.getString("NetworkOpeNum"));
		testInfoMap.put("NetworkOpeName",testInfo.getString("NetworkOpeName"));
		testInfoMap.put("NetworkISO",testInfo.getString("NetworkISO"));
		testInfoMap.put("NetworkType",testInfo.getString("NetworkType"));
	//	testInfoMap.put("",testInfo.getString(""));
		int ipAddressSize =testInfo.getStringArrayList("IpAddress").size();
		testInfoMap.put("IpAddress",testInfo.getStringArrayList("IpAddress").get(ipAddressSize-1));
		return testInfoMap;
	}

	static Handler myHandlerTcp = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			establishTCP();
		}
	};
	
	static class MyThreadTcp implements Runnable {
		public void run() {
			Message msg = new Message();
			TCPService.myHandlerTcp.sendMessage(msg);  
		}
	}
	
	static Handler myHandlerTcpFailed = new Handler() {
		@Override
		public void handleMessage(Message msg) {
		StringBuilder sb = new StringBuilder();
		sb.append("Connection Failed:\n");
		sb.append("\n-----------Please check your settings-----------\n");
		Toast.makeText(SMSreceiver.SMScontext, sb.toString(),
					Toast.LENGTH_LONG).show();

		}
	};
	static class MyThreadTcpFailed implements Runnable {
		public void run() {
			Message msg = new Message();
			TCPService.myHandlerTcpFailed.sendMessage(msg); 
		}
	}
	
	
	
	static Handler myHandlerTcpSuccess = new Handler() {
		@Override
		public void handleMessage(Message msg) {
		StringBuilder sb = new StringBuilder();
		sb.append("Connection Successful:\n");
		sb.append("\n-----------Start TCP-----------\n");
		Toast.makeText(SMSreceiver.SMScontext, sb.toString(),
					Toast.LENGTH_LONG).show();

		}
	};
	static class MyThreadTcpSuccess implements Runnable {
		public void run() {
			Message msg = new Message();
			TCPService.myHandlerTcpSuccess.sendMessage(msg);
			}
	}
	
}
